#!/usr/bin/env python3
"""
环境配置切换工具
用于在开发环境和生产环境之间切换
"""

import os
import shutil
import sys

def set_environment(env_type):
    """切换环境配置"""
    current_dir = os.path.dirname(os.path.abspath(__file__))
    source_file = os.path.join(current_dir, f'env.{env_type}')
    target_file = os.path.join(current_dir, '.env')
    
    if not os.path.exists(source_file):
        print(f"❌ 环境配置文件不存在: {source_file}")
        return False
    
    try:
        shutil.copy2(source_file, target_file)
        print(f"✅ 已切换到 {env_type} 环境")
        print(f"   配置文件: {source_file} -> {target_file}")
        
        # 显示当前环境配置
        print("\n📋 当前环境配置:")
        with open(target_file, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                if line and not line.startswith('#'):
                    print(f"   {line}")
        
        return True
    except Exception as e:
        print(f"❌ 切换环境失败: {e}")
        return False

def main():
    """主函数"""
    if len(sys.argv) != 2:
        print("使用方法:")
        print("  python set_env.py development  # 切换到开发环境")
        print("  python set_env.py production   # 切换到生产环境")
        return
    
    env_type = sys.argv[1].lower()
    
    if env_type not in ['development', 'production']:
        print("❌ 环境类型错误，只支持: development, production")
        return
    
    set_environment(env_type)

if __name__ == '__main__':
    main()
